﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using GNSCore.Managers;
using GNSWebSite.Managers;
using GNSWebSite.Constants;
using MSCaptcha;
using MailManager=GNSWebSite.Managers.MailManager;
using SettingsManager=GNSCore.Managers.SettingsManager;
using GNSWebSite.Settings;
using GNSCore.Repositories;
using GNSCore.Model;
using GNSCore.Model.Context;
using GNSCore.Repositories.IOC;

namespace GNSWebSite.Pages
{
    public partial class Register : System.Web.UI.Page
    {
        IRepository<Location> _localeRepository;
        IGNSContext _context;

        public string JustCreatedUserName
        {
            get
            {
                if(Session[SecuritySessionKeys.CreatedUser]!=null)
                {
                    return (string)Session[SecuritySessionKeys.CreatedUser];
                }
                return string.Empty;
            }
            set
            {
                Session[SecuritySessionKeys.CreatedUser] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitDependencies();
        }

        private void InitDependencies()
        {
            _localeRepository = IOCManager.IOCContainer.Resolve<IRepository<Location>>();
            _context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _localeRepository.SetContext(_context);
        }

        protected void btnSaveSettings_Click(object sender, EventArgs e)
        {
            ;
            if (Page.User != null)
            {
                TextBox email = (TextBox)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("Email");
                ucRegisterSettings.justCreatedUserName = JustCreatedUserName;
                ucRegisterSettings.SaveUserSettings();
                ucRegisterSettings.EMailAdress = email.Text;

                Response.Redirect("~/Pages/Default.aspx");
            }
        }
        protected void btnSavePosition_Click(object sender, EventArgs e)
        {
            if (Page.User != null)
            {
                LocationManger locationManager = new LocationManger(_localeRepository);
                locationManager.SaveLocationForUser(ucPlaceOnLiveMap.Latitude, ucPlaceOnLiveMap.Longtitude,
                                                   SecuritySettings.DefaultRadius, JustCreatedUserName);
                mvRegistration.ActiveViewIndex = 2;
            }
        }

        protected void cuwGNS_CreatedUser(object sender, EventArgs e)
        {
            TextBox username = (TextBox)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
            TextBox email = (TextBox)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("Email");
            string password = cuwGNS.Password;
            CreateUserWizard cuw = (CreateUserWizard)sender;
            MembershipUser user = System.Web.Security.Membership.GetUser(cuw.UserName);
            Guid userID = (Guid)user.ProviderUserKey;
            JustCreatedUserName = username.Text;
            MailManager.SendConfirmEmail(username.Text,userID,password,email.Text);
            mvRegistration.ActiveViewIndex = 1;
        }

        protected void cvCAPTCHA_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CaptchaControl ucNoBot = (CaptchaControl)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("ucNoBot");
            TextBox tbCAPTCHA = (TextBox)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("tbCAPTCHA");

            ucNoBot.ValidateCaptcha(tbCAPTCHA.Text);
            if (!ucNoBot.UserValidated)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void cuwGNS_UserCreating(object sender, LoginCancelEventArgs e)
        {
            CustomValidator cvCAPTCHA = (CustomValidator)cuwGNS.CreateUserStep.ContentTemplateContainer.FindControl("cvCAPTCHA");
            cvCAPTCHA.Validate();
            if(!cvCAPTCHA.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }
    }
}
